package util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    private static final ServiceRegistry serviceRegistry;
    
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            //sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        	
        	Configuration cfg = new Configuration();

			cfg.addResource("pojo/Binhluan.hbm.xml").configure();
			cfg.addResource("pojo/Chitietgiohang.hbm.xml").configure();
			cfg.addResource("pojo/Chitiethoadon.hbm.xml").configure();
			cfg.addResource("pojo/Giohang.hbm.xml").configure();
			cfg.addResource("pojo/Hinhthucthanhtoan.hbm.xml").configure();
			cfg.addResource("pojo/Hoadon.hbm.xml").configure();
			cfg.addResource("pojo/Loaisanpham.hbm.xml").configure();
			cfg.addResource("pojo/Makiemtra.hbm.xml").configure();
			cfg.addResource("pojo/Nhasanxuat.hbm.xml").configure();
			cfg.addResource("pojo/Nhomsanpham.hbm.xml").configure();
			cfg.addResource("pojo/Quangcao.hbm.xml").configure();
			cfg.addResource("pojo/Sanpham.hbm.xml").configure();
			cfg.addResource("pojo/Taikhoan.hbm.xml").configure();
			cfg.addResource("pojo/Taikhoanadmin.hbm.xml").configure();
			cfg.addResource("pojo/Thamsochung.hbm.xml").configure();
			
			serviceRegistry = new ServiceRegistryBuilder().applySettings(
					cfg.getProperties()).buildServiceRegistry();

			sessionFactory = cfg.buildSessionFactory(serviceRegistry);
			
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}